package org.blame.lotterysystem.service.activityStatus.impl;

import org.blame.lotterysystem.common.errorcode.ServiceErrorCodeConstants;
import org.blame.lotterysystem.common.exception.ServiceException;
import org.blame.lotterysystem.service.ActivityService;
import org.blame.lotterysystem.service.activityStatus.ActivityStatusManager;
import org.blame.lotterysystem.service.activityStatus.operator.AbstractActivityOperator;
import org.blame.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Component
public class ActivityStatusManagerImpl implements ActivityStatusManager {

    private static final Logger logger= LoggerFactory.getLogger(ActivityStatusManagerImpl.class);

    @Autowired
    private final Map<String, AbstractActivityOperator> operatorMap=new HashMap<>();

    @Autowired
    private ActivityService activityService;

    @Override
    public void handlerEvent(ConvertActivityStatusDTO convertActivityStatusDTO) {
        if (CollectionUtils.isEmpty(operatorMap)){
            logger.warn("operatorMap为空");
            return ;
        }

        Map<String,AbstractActivityOperator> currMap=new HashMap<>(operatorMap);
        boolean update=false;
//        先处理人员，奖品
        update=processConvertStatus(convertActivityStatusDTO,currMap,1);
//        后处理活动
        update=processConvertStatus(convertActivityStatusDTO,currMap,2)||update;
//        进行更新缓存
        if (update) {
            activityService.cacheActivity(convertActivityStatusDTO.getActivityId());
        }
    }

    @Override
    public void rollbackHandlerEvent(ConvertActivityStatusDTO convertActivityStatusDTO) {
        for (AbstractActivityOperator operator : operatorMap.values()) {
            operator.convert(convertActivityStatusDTO);
        }

        // 缓存更新
        activityService.cacheActivity(convertActivityStatusDTO.getActivityId());

    }

    private Boolean processConvertStatus(ConvertActivityStatusDTO convertActivityStatusDTO, Map<String, AbstractActivityOperator> currMap, int sequence) {
        boolean update=false;
//        遍历currMap
        Iterator<Map.Entry<String,AbstractActivityOperator>> iterator=currMap.entrySet().iterator();
        while (iterator.hasNext()){
            AbstractActivityOperator operator=iterator.next().getValue();
            if(operator.sequence()!=sequence || !operator.needConvert(convertActivityStatusDTO)){
                continue;
            }
            if(!operator.convert(convertActivityStatusDTO)){
                logger.error("{}转换失败",operator.getClass().getName());
                throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_STATUS_CONVERT_ERROR);

            }
            iterator.remove();
            update=true;
        }
        return update;
    }
}
